const app = getApp()
const WXAPI = require("../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 0, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '我的会员码', // 导航栏 中间的标题
      textColor: '#fff', // 标题颜色
      bgColor: '', // 导航栏背景颜色
      isColor: true,
    },
    info: {}, //信息
    time: 120, //倒计时时间
    isRefresh: false, //是否刷新
    isScanCode: false, //是否已经扫码
    token:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getQrCode()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.login_dialog = this.selectComponent("#login_dialog");
    // this.login_dialog.loginHide
    this.setData({
      token: wx.getStorageSync('token')
    })
    if (this.data.token) {
      this.getQrCode()
    }else{
      this.setData({
        info:{avatar:''}
      })
    }
  },
  jumpLogin(){ //登录
    this.login_dialog.loginShow()
  },
  //登录成功
  loginSuccess(){
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.getQrCode()
  },
  //跳转我的预付卡
  jumpMycard(){
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/allCard/allCard',
    })
  },
  // 刷新重试
  getRefresh() {
    clearInterval(this.data.timer)
    clearInterval(this.data.codeTime)
    this.setData({
      isRefresh: true,
      time: 120
    })
    this.getQrCode()
  },
  //获取会员码
  getQrCode() {
    clearInterval(this.data.timer)
    var data = {}
    WXAPI.memberQrcode(data, (res) => {
      if (res.code == 200) {
        this.setData({
          info: res.result,
        })
        this.circulateGetQrcodeScan()
        setTimeout(() => {
          this.setData({
            isRefresh: false,
          })
        }, 1000)
        this.countdownView()
      }
    })
  },
  //轮询请求二维码使用情况
  circulateGetQrcodeScan() {
    var that = this
    this.setData({
      codeTime: setInterval(function () {
        that.getQrcodeScan()
      }, 2000)
    })
  },
  //查询二维码使用情况
  getQrcodeScan() {
    var data = {
      query: this.data.info.query
    }
    WXAPI.qrcodeScan(data, (res) => {
      this.setData({
        isScanCode: res.result
      })
      if (res.result) {
        clearInterval(this.data.codeTime);
      }
    })
  },
  //倒计时
  countdownView() {
    var that = this
    this.setData({
      timer: setInterval(function () {
        that.data.time--;
        that.setData({
          time: that.data.time,
        })
        if (that.data.time == 0) {
          clearInterval(that.data.timer);
          that.setData({
            time: 120
          })
          that.getQrCode()
        }
      }, 1000)
    })
  },
  bindscroll(e) { //监听页面滚动距离
    var that = this;
    var scrollY = e.detail.scrollTop
    if (scrollY > 50) {
      that.setData({
        ['nvabarData.textColor']: '#000',
        ['nvabarData.isScrollY']: true,
        ['nvabarData.isColor']: true,
      })
      wx.setNavigationBarColor({
        frontColor: '#000000',
        backgroundColor: '#000000',
      })
    } else if (scrollY < 50) {
      that.setData({
        ['nvabarData.textColor']: '#fff',
        ['nvabarData.isScrollY']: false,
        ['nvabarData.isColor']: false,
      })
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#ffffff',
      })
    }
  },
  onHide() {
    console.log(2121)
    clearInterval(this.data.timer)
    clearInterval(this.data.codeTime)
    this.setData({
      time: 120
    })
  }
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh() {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom() {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})